**********************************************************
*** Shock Analysis ***
**********************************************************
bysort ind_id year month: egen sum_benefits = sum(benefits)
bysort ind_id year month: egen sum_pharm = sum(pharm)
bysort ind_id year month: keep if _n == 1

* 1.1 Unemployment shock
gen unemp_shock = 0
replace unemp_shock = 1 if sum_benefits > 0 
bysort ind_id: egen total_benefits = sum(sum_benefits)
gsort ind_id -unemp_shock year month
bysort ind_id: gen firstmonth = month[1]
bysort ind_id: gen firstyear = year[1]
replace firstmonth = 0 if total_benefits == 0
replace firstyear = 0 if total_benefits == 0
gen unemp_shock2 = 0
replace unemp_shock2 = 1 if unemp_shock == 1 & year == firstyear & month == firstmonth
sort ind_id year month
drop firstmonth firstyear

* 1.2 Health shock
gen mul = 0
forvalues i=2/29{
	bysort ind_id: replace mul = sum_pharm[`i']/sum_pharm[`i'-1] if _n == `i'
	replace mul = 0 if sum_pharm == 0
	replace mul = 2.1 if mul == .
}
gen health_shock = 0
replace health_shock = 1 if mul > 3 & sum_pharm > 15000
drop mul


* 1.3 Income shock
gen income_shock = 0
gen income_shock2 = 0
forvalues i=1/29{
	gen mul = 0
	bysort ind_id: replace mul = 1-month_total_income/month_total_income[`i'] if _n > `i'
	replace mul = 0 if mul == .
	gen ifmul = 0
	replace ifmul = 1 if mul >= 0.25
	local k = `i' + 2
	forvalues j = `k'/29{
		bysort ind_id: replace ifmul = 0 if ifmul[`j'-1] == 0 & _n == `j'
	} 
	bysort ind_id: egen frequency=sum(ifmul)
	bysort ind_id: replace income_shock = 1 if frequency >= 3 & ifmul == 1
	
	gsort ind_id -ifmul year month
	bysort ind_id: gen firstmonth = month[1]
	bysort ind_id: gen firstyear = year[1]
	replace firstmonth = 0 if frequency < 3
	replace firstyear = 0 if frequency < 3
	bysort ind_id: replace income_shock2 = 1 if frequency >= 3 & ifmul == 1 & year == firstyear & month == firstmonth
	sor ind_id year month
	drop mul ifmul frequency firstmonth firstyear
}


**********************************************************
*Table 4: Individual Shocks and Onset of Co-Holding
*A: Summary Statistics
*B: OLS Regression
**********************************************************	
sort ind_id year month
foreach var in unemp_shock2 health_shock income_shock2{
		forvalues i = 1/3{
			bysort ind_id: gen L`i'_`var' = `var'[_n-`i']
		}
}
reghdfe pr_cohold_start_reg L1_* if coholder==1, noabsorb
est store regL1

generate sample = e(sample)
summarize unemp_shock2 if sample == 1
summarize health_shock if sample == 1
summarize income_shock2 if sample == 1 

reghdfe pr_cohold_start_reg L1_* L2_* L3_* if coholder==1, noabsorb
est store regL2

merge m:1 edate using bank_rates.dta
gen unemp_spread = L1_unemp_shock2*spread
gen health_spread = L1_health_shock*spread
gen income_spread = L1_income_shock2*spread

reghdfe pr_cohold_start_reg L1_* unemp_spread health_spread income_spread spread if coholder==1, noabsorb
est store regL3

esttab regL* using baseline.rtf, replace ///
	star( * 0.10 ** 0.05 *** 0.01 ) nogaps obslast compress /// 
	order(L1_* L2_* L3_* unemp_spread health_spread income_spread spread) ///
	b(4) se(4)  r2(4) ar2 ///
	indicate(`r(indicate_fe)') mtitles() ///
	title(Table 4: Individual Shocks and Onset of Co-Holding)
	

**********************************************************
*Table A17: OLS Regression Estimates: Shocks and Coholding, Alternative Definition of Health Shock
**********************************************************		
gen mul = 0
forvalues i=2/29{
	bysort ind_id: replace mul = sum_pharm[`i']/sum_pharm[`i'-1] if _n == `i'
	replace mul = 0 if sum_pharm == 0
	replace mul = 2.1 if mul == .
}
gen health_shock2 = 0
replace health_shock2 = 1 if mul > 4 & sum_pharm > 150000 //a modified definition of health shock based upon a quadrupling of health expenditure
drop mul

sort ind_id year month
foreach var in unemp_shock2 health_shock2 income_shock2{
		forvalues i = 1/3{
			bysort ind_id: gen L`i'_`var' = `var'[_n-`i']
		}
}
reghdfe pr_cohold_start_reg L1_* if coholder==1, noabsorb
est store regL1

reghdfe pr_cohold_start_reg L1_* L2_* L3_* if coholder==1, noabsorb
est store regL2

merge m:1 edate using bank_rates.dta
gen unemp_spread2 = L1_unemp_shock2*spread
gen health_spread2 = L1_health_shock2*spread
gen income_spread2 = L1_income_shock2*spread

reghdfe pr_cohold_start_reg L1_* unemp_spread2 health_spread2 income_spread2 spread if coholder==1, noabsorb
est store regL3

esttab regL* using baseline.rtf, replace ///
	star( * 0.10 ** 0.05 *** 0.01 ) nogaps obslast compress /// 
	order(L1_* L2_* L3_* unemp_spread health_spread income_spread spread) ///
	b(4) se(4)  r2(4) ar2 ///
	indicate(`r(indicate_fe)') mtitles() ///
	title(Table A17: OLS Regression Estimates: Shocks and Coholding, Alternative Definition of Health Shock)

	
	
**********************************************************
*Table 3: Comparison of Co-Holders and Non Co-Holders
*A: Summary Statistics
*B: OLS Regression 
**********************************************************		
sort ind_id edate
generate income_new = month_total_income_mean/1000000

*t-test 
generate sample = e(sample)
asdoc ttest age if sample==1, by(coholder)
asdoc ttest female if sample==1, by(coholder)
asdoc ttest income_new if sample==1, by(coholder)
asdoc ttest alwaysin2memberhh if sample==1, by(coholder)
asdoc ttest benefits_pers if sample==1, by(coholder)

* OLS
drop if new_ind_id==11548|new_ind_id==4821|new_ind_id==388|new_ind_id==2315 //drop some outliers with exceptionally high income 
eststo reg: reg coholder age female income_new alwaysin2memberhh benefits_pers if edate_id==1456,robust
esttab reg, se r2 star(* 0.10 ** 0.05 *** 0.01)



**********************************************************
*Table A12: Summary Statistics for Baseline Sample
**********************************************************	
summarize age female income_new alwaysin2memberhh benefits_pers if sample==1, detail


